<?php
if (!defined('__BACKEND__')) die ("you cannot directly access this page");
/*
 * Create by Sony K Wibisono,S.Si
 * e-mail : sony@sonywibisono.com
 * Do not copy without permission
 */
include ("class/AppUsers.php");
include ("class/UserRoles.php");
function add(){
    global $db;
    $logintype=  select_login_type('');
echo "
<div id=\"entryForm\">
<h1>New User Registration</h1>
<form action=\"?mod=users&file=register\" method=\"post\">
   
    <input type=\"hidden\" name=\"action\" value=\"add_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td class=\"field_name\">Id</td><td class=\"field_object\"># </td></tr>
<tr><td class=\"field_name\">User Name</td><td class=\"field_object\">
<input type=\"text\" name=\"user_name\" id=\"user_name\" size='20'/><span id=\"msg_user\"></span> </td></tr>
<tr><td class=\"field_name\">Password</td>
<td class=\"field_object\"><input id=\"pass_word\" type=\"password\" name=\"pass_word\" size='15'  /> </td></tr>
<tr><td class=\"field_name\">Verify Your Password</td><td class=\"field_object\">
<input id=\"verify\" type=\"password\" name=\"verify\" size='15' /> 
<img id=\"msg_pass\" class=\"icon_small\" src=\"\" title=\"Verify Status\"></td></tr>

<tr><td class=\"field_name\">Full Name</td><td class=\"field_object\"><input type=\"text\" name=\"full_name\" /> </td></tr>
<tr><td class=\"field_name\">Email</td><td class=\"field_object\"><input id=\"email\" type=\"text\" name=\"email\" /> 
<span id=\"msg_email\"></span></td></tr>
<tr><td class=\"field_name\">Mobile</td>
<td class=\"field_object\"><input id=\"mobile\" type=\"text\" name=\"mobile\" /> 
<span id=\"msg_mobile\"></span></td></tr>
<tr><td class=\"field_name\">Sms Alert Receiver</td><td class=\"field_object\">
<input type=\"radio\" name=\"sms_alert_receiver\" value=\"Y\" />Yes
<input type=\"radio\" name=\"sms_alert_receiver\" value=\"N\" />No
</td></tr>
<tr><td class=\"field_name\">Social Media Login</td><td class=\"field_object\"><input type=\"text\" name=\"social_media_login\" /> </td></tr>
<tr><td class=\"field_name\">Social Media Type</td>
<td class=\"field_object\">$logintype </td></tr>
<tr><td class=\"field_name\">Register as</td>
<td class=\"field_object\"><input type=\"radio\" name=\"regtype\" value=\"customer\"/>Customer
<input type=\"radio\" name=\"regtype\" value=\"support\"/>Technical Support
</td></tr>    
<tr><td>&nbsp;</td><td><img id=\"captcha\" src=\"securimage/securimage_show.php\" alt=\"CAPTCHA Image\" /></td></tr>
        <tr><td class=\"field_name\">Type word above</td> <td><input type=\"text\" class=\"login_text\" name=\"CAPTCHA\" 
        size=\"10\" maxlength=\"6\"></td></tr>
<tr><td></td><td><input class=\"button\" type=\"submit\" value=\"Kirim\"/>
        <input class=\"button\" type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>
";
?>
</div>
<script>
function isEmail(email) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return regex.test(email);
}
function isNumber(number){
    var regex=/^[0-9]\d*$/;
    return regex.test(number);
}
</script>
<script>
    $("#verify").keyup(
    function (){
        var value2=$(this).val();
        var value1=$("#pass_word").val();
        if (value1!=value2){
            $("#msg_pass").attr('src','images/not_ok.png');
            $("#msg_pass").attr('title','Password incorrect!');
        } else {
            $("#msg_pass").attr('src','images/ok.png');
            $("#msg_pass").attr('title','Password corrected!');
        }
    });
    $("#user_name").keyup(function(){
        var value=$(this).val();
        $("#msg_user").load('?mod=users&file=register&action=check&notheme=1&username='+value);
    });
    $("#email").keyup(function(){
        var value=$(this).val();
        if (isEmail(value)){
            $("#msg_email").text('Email Correct!');    
        } else {
            $("#msg_email").text('Email Not Correct!');
        }
        
    });
    $("#mobile").keyup(function(){
        var value=$(this).val();
        if (isNumber(value)){
           $("#msg_mobile").text('Mobile Number Correct!');  
        } else {
           $("#msg_mobile").text('Mobile Number Not Correct!');  
        }
    });
</script>
<?php
}
function notification_body(){
    global $username,$password,$fullname,$email,$profile,
            $mobile,$messenger,$messenger_id;
    $message = "<html>
<head>
  <title>User Notification</title>
</head>
<body>
  <p>New User Registration!</p>
  <table>
    <tr><th>Name</th><td>$fullname</td></tr>
    <tr><th>Login Name</th><td>$username</td></tr>
    <tr><th>E-mail</th><td>$email</td></tr>
    <tr><th>Profile</th><td>$profile</td></tr>
    <tr><th>No. Handphone</th><td>$mobile</td></tr>
    <tr><th>Messenger Type</th><td>$messenger</td></tr>
    <tr><th>Messenger ID</th><td>$messenger_id</td></tr>
  </table>
</body>
</html>";

  return $message;

}
function add_process(){
    global $db,$user_name,$pass_word,$full_name,$email,$mobile,$securimage,
            $active_session,$locked,$active,$sms_alert_receiver,
            $social_media_login,$login_type,$CAPTCHA,$regtype;
    
    if ($securimage->check($CAPTCHA) == false) {
     $msg= "The security code entered was incorrect.<br /><br />";
     $msg.= "Please go <a href='javascript:history.go(-1)'>back</a> and try again.";
     error_msg($msg);
     exit;
    } else {
    $user=new AppUsers();
    $user->user_name=$user_name;
    $user->pass_word=md5($pass_word);
    $user->full_name=$full_name;
    $user->sms_alert_receiver=$sms_alert_receiver;
    if (!empty($social_media_login)){
    $user->social_media_login=$social_media_login;
    $user->social_media_type=$login_type;
    
    }
    $user->locked="Y";
    $user->active="N";
    $user->email=$email;
    if ($regtype=='customer'){
        $role_id=5;
    } else {
        $role_id=2;
    }
     
    if ($user->is_exist($user_name)){
        error_msg("User already Exists!");
    } else {
        $userid=$user->insert();
        $user_role=new UserRoles();
        $user_role->app_role_id=$role_id;
        $user_role->app_user_id=$userid;
        $user_role->insert();
//        echo "SMS alert : $sms_alert_receiver";
//        echo "Register Type : $regtype";
        echo "New User Registration Success,please read your e-mail to activate your account.";
        $body=notification_body();
     $subject="New User Notification";
     $from="sony@sonywibisono.com";
     $to=$email;
     $cc="wibisono.sony@gmail.com";
     $bcc='';
     send_mail($from, $to, $cc, $bcc, $subject, $body);
    }
      
     
     
     
     //refresh_to("index.php?mod=search", "Anda dialihkan ke Halaman Pencarian!", 3);
     
    }
}
function check_user(){
    global $db,$username;
    $sql="select user_name,full_name from app_users where user_name='$username' ";
    $row=$db->fetch_row($sql);
    if (!empty($row)){
        echo "User Not Available!";
    } else {
        echo "User Available!";
    } 
        
}
global $action;
switch ($action){
    case 'add_process':
        add_process();
        break;
    case 'check':
        check_user();
        break;
    default :
        add();
}
?>
